//app.js
App({
    onLaunch: function () {
        this.getToken()
    },
    globalData: {
        baseurl: "http://127.0.0.1:9090/api/",
        // baseurl: "http://159.75.82.221/api/",
        token: wx.getStorageSync('token'),
        userInfo: undefined,
    },
    getToken: function () {
        const that = this
        const token = that.globalData.token
        if (!token) {
            wx.login({
                success(res) {
                    if (res.code) {
                        wx.request({
                            url: that.globalData.baseurl + "user/getToken",
                            method: "POST",
                            data: {code: res.code},
                            success: function (res) {
                                const token = res.data.data
                                if (res.data.code === 200) {
                                    wx.setStorageSync('token', token)
                                    that.getUserInfoByToken(token)
                                }
                                wx.getSetting({
                                    success: res => {
                                        if (res.authSetting["scope.userInfo"]) {
                                            wx.getUserInfo({
                                                success: res => {
                                                    const userInfo = res.userInfo
                                                    that.globalData.userInfo = userInfo
                                                    wx.request({
                                                        url: that.globalData.baseurl + "user/upUser",
                                                        method: "POST",
                                                        data: {
                                                            token: token,
                                                            nickName: userInfo.nickName,
                                                            avatarUrl: userInfo.avatarUrl,
                                                            gender: userInfo.gender
                                                        },
                                                    })
                                                }
                                            })
                                        }
                                    }
                                })
                            }
                        })
                    }
                }
            })
        } else {
            wx.request({
                url: that.globalData.baseurl + "user/updateToken",
                method: "POST",
                data: {token: token},
                success: function (res) {
                    if (res.data.code === 200) {
                        wx.setStorageSync('token', res.data.data)
                    }
                }
            })
            that.getUserInfoByToken(token)
        }
    },
    getUserInfoByToken: function (token) {
        const that = this
        wx.request({
            url: that.globalData.baseurl + "user/getUserInfoByToken",
            method: "POST",
            data: {token: token},
            success: function (res) {
                if (res.data.code === 200) {
                    that.globalData.userInfo = res.data.data
                }
            }
        })
    },
})
